<?php
namespace App\Service;

use App\Models\ConfigureFunction; //功能配置表

use GuzzleHttp\Client;

class WdhService
{

    private static function request($path, $args_data = [])
    {

        //http实例
        $client = new Client();

        //查询商城配置域名
        $domain_url = ConfigureFunction::getConfigureField(43, 'domain_url');

        $res = $client->get($domain_url . $path, ['query' => $args_data]);

        if ($res->getStatusCode() != 200) {
            return ['code' => 10001, 'message' => '接口调用失败'];
        }

        return json_decode($res->getBody()->getContents(), true);

    }

    /**
     * 查询余额
     * @param $args_data
     * @return float
     */
    public static function balance( $args_data )
    {

        $res = self::request('/app/api/get_balance.php', $args_data);

        if ($res['code'] != 200) {
            return 0.00;
        }

        return $res['data'];

    }

    /**
     * 查询积分
     * @param array $args_data
     * @return int
     */
    public static function points( $args_data )
    {

        $res = self::request('/app/api/get_points.php', $args_data);

        if ($res['code'] != 200) {
            return 0;
        }

        return $res['data'];

    }


    /**
     * 更新crm会员资料
     * @param array $args_data
     * @return int
     */
    public static function customerEdit( $args_data )
    {

        $res = self::request('/app/api/customer_edit.php', $args_data);

        if ($res['code'] != 200) {
            return 0;
        }

        return $res;

    }



    /**
     * 发送短信
     * @param $args_data
     * @return array
     */
    public static function sendSms( $args_data )
    {

        if ( !isset($args_data['mobile']) || empty($args_data['mobile']) || !isMobile($args_data['mobile']) ) {
            return ['code' => 100001, 'message' => '手机号错误'];
        }

        if ( !isset($args_data['template']) || empty($args_data['template']) ) {
            return ['code' => 100002, 'message' => '短信模板不能为空'];
        }

        if ( !isset($args_data['session_id']) || empty($args_data['session_id']) ) {
            return ['code' => 100002, 'message' => 'session_id不能为空'];
        }

        $res = self::request('/app/api/send_sms.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100003, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok', 'data' => $res['data']];

    }

    /**
     * 会员开卡接口
     * @param $args_data
     * @return array
     */
    public static  function openCard( $args_data )
    {

        $res = self::request('/app/api/open_card.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100001, 'message' => '开卡接口调用失败'];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


    /**
     * 余额增加（充值）
     * @param array $args_data
     * @return array
     */
    public static function recharge ( $args_data )
    {

        $res = self::request('/app/api/add_balance.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }

    /**
     * 查询和校验充值卡（线下）
     * @param array $args_data
     * @return array
     */
    public static function validateQuery ( $args_data )
    {

        $res = self::request('/app/api/get_recharge_card.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;

    }

    /**
     * 线下充值卡充值
     * @param array $args_data
     * @return array
     */
    public static function transfer ( $args_data )
    {

        $res = self::request('/app/api/get_transfer', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;

    }

    /**
     * 积分增加
     * @param array $args_data
     * @return array
     */
    public static function pointsAdd ( $args_data )
    {

        $res = self::request('/app/api/add_points.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }



    /**
     * 扣减余额
     * @param array $args_data
     * @return array
     */
    public static function deductionBalance($args_data)
    {

        $res = self::request('/app/api/deduction_balance.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


    /**
     * 扣减积分
     * @param array $args_data
     * @return array
     */
    public static function deductionPoints($args_data)
    {

        $res = self::request('/app/api/deduction_points.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }

    /**
     * 推送团购订单支付信息
     * @param array $args_data
     * @return array
     */
    public static function ptOrderPay($args_data)
    {

        $res = self::request('/app/async/order/pt_order_pay.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }

    /**
     * 推送在线支付信息
     * @param array $args_data
     * @return array
     */
    public static function onlinePaySend($args_data)
    {

        $res = self::request('/app/api/online_send.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return array('code' => 200, 'message' => 'ok', 'data' => $res['data']);

    }

    /**
     * 推送在线支付退款信息
     * @param array $args_data
     * @return array
     */
    public static function onlinePayRefund($args_data)
    {

        $res = self::request('/app/api/onlinepay_refund.php', $args_data);

        return $res;

    }

    /**
     * 检查配送范围严格模式
     * @param $args_data
     * @return array
     */
    public static function checktrictSendRange($args_data)
    {

        if ( !isset($args_data['mallID']) || empty($args_data['mallID']) ) {
            return array('code'=>100000, 'message'=>'门店ID不能为空');
        }

        if ( !isset($args_data['longitude'], $args_data['latitude']) || empty($args_data['longitude']) || empty($args_data['latitude']) ) {
            return array('code'=>100001, 'message'=>'经纬度不能为空');
        }

        $res = self::request('/app/api/check_send_range.php', $args_data);
        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return array('code' => 200, 'message' => 'ok', 'data' => $res['data']);

    }


    /**
     * 验证支付密码
     * @param array $args_data
     * @return array
     */
    public static function checkPwd($args_data)
    {

        $res = self::request('/app/api/check_pwd.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];

    }


    /**
     * 获取微信卡券的ticket
     * @param
     * @return array|mixed
     */
    public static function getWxTicket( )
    {

        $res = self::request('/app/api/get_wx_ticket.php');

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res['data'];

    }

    /**
     * 查询积分变更记录
     * @param array $args_data
     * @return int
     */
    public static  function pointsLogs( $args_data )
    {

        $res = self::request('/app/api/points_logs.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res['data'];

    }


    /**
     * 查询余额变更记录
     * @param array $args_data
     * @return int
     */
    public static  function balanceLogs( $args_data )
    {

        $res = self::request('/app/api/balance_logs.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res['data'];

    }

    /**
     * 微电汇订单取消
     * @param $args_data
     * @return mixed
     */
    public static function cancel( $args_data )
    {

        $res = self::request('/app/api/bill_cancel.php', $args_data);

        return $res;

    }

    /**
     * 获取省市区信息
     * @return array
     */
    public static  function getRegion( )
    {

        $res = self::request('/app/api/sys_region.php');

        if ($res['code'] != 200) {
            return null;
        }

        return $res['data'];

    }

    /**
     * 发送消息模板
     * @param $args_data
     * @return mixed|null
     */
    public static function sendMessage( $args_data ) {

        $res = self::request('/async/message/message.php',$args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];
    }

    public static function asycPush( $args_data ) {


        $res = self::request('/app/api/asyncPush.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return ['code' => 200, 'message' => 'ok'];
    }

    /**
     * 获取商品促销|订单促销
     * @param $args_data
     * @return array
     */
    public static function getPromotion( $args_data ) {

        //参数检查 ptype：1. 商品促销 2. 订单促销
        if (!isset($args_data['ptype']) || !in_array($args_data['ptype'], [1, 2])) {
            $args_data['ptype'] = 1;
        }

        $res = self::request('/app/api/get_promotion.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100001, 'message' => $res['message']];
        }

        return $res['data'];

    }

    /*
     * pc商管家外卖平台取消订单
     * @param $args_data
     * @return array
     */
    public static function bill_takeout($args_data){

        $res = self::request('/app/api/bill_takeout.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;
    }

    /*
     * pc商管家外卖平台订单完成
     * @param $args_data
     * @return array
     */
    public static function bill_finish($args_data){

        $res = self::request('/app/api/bill_finish.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;
    }

    /*
     * pc商管家查询订单支付信息
     * @param $args_data
     * @return array
     */
    public static function bill_pay($bill_no){

        $res = self::request('/app/api/bill_pay.php', $args_data= ['bill_no'=>$bill_no]);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;
    }

    /*
     * pc商管家生成退款单
     * @param $args_data
     * @return array
     */
    public static function refund_single($args_data){

        $res = self::request('/app/api/refund_single.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;
    }

    /*
     * pc商管家订单退单
     * @param $args_data
     * @return array
     */
    public static function backOrder($args_data){

        $res = self::request('/app/api/bill_back.php', $args_data);

        if ($res['code'] != 200) {
            return ['code' => 100000, 'message' => $res['message']];
        }

        return $res;
    }


    /*
     * pc商管家订单退单
     * @param $args_data
     * @return array
     */
    public static function pushBillToErp($args_data){

        self::request('/app/api/push_bill_erp.php', $args_data);
    }

    /*
     *pc商管家同步外卖平台订单
     * @param $args_data
     * @return array
     *
     */
    public static function takeoutUpstream($args_data){
        return self::request('/app/api/takeout_bill.php', $args_data);
    }

    /**
     * 支付宝退款
     * @param array $args_data
     * @return int
     */
    public static  function alipayRefund( $args_data )
    {

        $res = self::request('/onlinepay/alipay/refund.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res;

    }

    /**
     * 微信退款
     * @param array $args_data
     * @return int
     */
    public static  function weixinRefund( $args_data )
    {

        $res = self::request('/onlinepay/weixin/refund.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res;

    }

    /**
     * 百付宝退款
     * @param array $args_data
     * @return int
     */
    public static  function baifubaoRefund( $args_data )
    {

        $res = self::request('/onlinepay/baifubao/refund.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res;

    }

    /**
     * 银联退款
     * @param array $args_data
     * @return int
     */
    public static  function unionpayRefund( $args_data )
    {

        $res = self::request('/onlinepay/unionpay_wap/refund.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res;

    }


    /**
     * 卡券发放
     * @param array $args_data
     * @return int
     */
    public static  function codeHandle( $args_data )
    {

        $res = self::request('/app/api/code_handle.php', $args_data);

        if ($res['code'] != 200) {
            return null;
        }

        return $res;

    }
}